package org.game.module.friendsbo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bean.FriendInfo;
import org.bean.PlayerInfo;
import org.constant.FriendConstant;
import org.error.GameErrorCode;
import org.game.constant.SessionConstent;
import org.game.db.bean.Player;
import org.game.db.bean.PlayerData;
import org.game.db.proxy.bean.PlayerDataProxy;
import org.game.db.vo.PlayerDataVO.DiscreteVO;
import org.game.msgBuilder.FriendMsgBuilder;
import org.game.protobuf.c2s.C2SFriendMsg.ReqOperate;
import org.game.protobuf.c2s.C2SFriendMsg.ReqQueryPlayer;
import org.game.protobuf.s2c.S2CFriendMsg.RespFriends;
import org.game.protobuf.s2c.S2CFriendMsg.RespOperate;
import org.game.protobuf.s2c.S2CFriendMsg.RespQueryPlayer;
import org.game.protobuf.s2c.S2CFriendMsg.RespRecommendFriends;
import org.game.protobuf.s2c.S2CFriendMsg.RespRecommendFriends.Builder;
import org.game.protobuf.s2c.S2CShareMsg.ChangeType;
import org.game.util.ProtoUnit;
import org.utils.RemoteUtil;

import com.sojoys.artifact.exception.AbstractLogicModelException;
import com.sojoys.artifact.factory.session.ISession;
import com.sojoys.artifact.tools.ToolError;
import com.sojoys.artifact.tools.ToolMap;
import com.xiaoleilu.hutool.date.DateUnit;

/**
 * @author : DengYing
 * @CreateDate : 2017年12月4日 下午2:36:24
 * @Description ：Please describe this document
 */
public class FriendsBOImpl implements FriendsBO {

	@Override
	public byte[] getFriends(ISession session) throws AbstractLogicModelException {
		Player player = session.getAttr(SessionConstent.PLAYER);
		List<FriendInfo> friends = RemoteUtil.socialSync().getFriends(player.getId());
		return ProtoUnit.toByte(FriendMsgBuilder.createRespFriendsMsg(player, friends));
	}

	@Override
	public byte[] operate(ISession session, ReqOperate msg) throws AbstractLogicModelException {
		Player player = session.getAttr(SessionConstent.PLAYER);
		Map<Object, Object> ext = new HashMap<>(1);
		switch (msg.getType()) {
		case FriendConstant.OPERATE_RECEIVE_GIFT:
			PlayerData playerData = player.getPlayerDatasFkPlayerId();
	    	PlayerDataProxy playerDataProxy = playerData.getPlayerDataProxy();
	    	DiscreteVO discreteVO = playerDataProxy.getDiscreteVO();
			ext.put("lastReceiveTime", discreteVO.getLastGetFriendGiftDate());
			ext.put("receiveCount", discreteVO.getNowGetFriendGiftCount());
			break;
		}
		RemoteUtil.socialSync().operate(player.getId(), msg.getTargetsList(), msg.getType(),ext);
		return ProtoUnit.toByte(RespOperate.newBuilder().addAllTargets(msg.getTargetsList()).setType(msg.getType()).build());
	}
	

	@Override
	public byte[] getRecommendFriends(ISession session) throws AbstractLogicModelException {
		Player player = session.getAttr(SessionConstent.PLAYER);
		// 取得最后获得推荐好友时间
		long lastTime = player.getPlayerProxy().getLastORFT();
		
		if (System.currentTimeMillis() - lastTime < 5 * DateUnit.SECOND.getMillis()) {
			throw ToolError.newLogicError(GameErrorCode.THE_OPERATION_IS_TOO_FREQUENT);
		}
		List<PlayerInfo> playerInfos = RemoteUtil.socialSync().getRecommendFriends(player.getId());
		player.getPlayerProxy().setLastORFT(System.currentTimeMillis());
		RespRecommendFriends resp = RespRecommendFriends.newBuilder().addAllFriends(FriendMsgBuilder.createRecommendFriendInfo(playerInfos)).build();
		return ProtoUnit.toByte(resp);
	}


	@Override
	public byte[] queryPlayerByID(ISession session, ReqQueryPlayer msg) throws AbstractLogicModelException {
		PlayerInfo playerInfo = RemoteUtil.socialSync().getPlayerInfo(msg.getPid());
		List<PlayerInfo> infos = new ArrayList<>(1);
		infos.add(playerInfo);
		
		RespQueryPlayer.Builder resp = RespQueryPlayer.newBuilder().addAllFriends(FriendMsgBuilder.createRecommendFriendInfo(infos));
		return ProtoUnit.toByte(resp.build());
	}
}
